﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using SecureCommander.Core.UserManagement;
using Microsoft.Win32;
using SecureCommander.DataAccess;
using System.Security.Cryptography;
using System.IO;
using SecureCommander.Helper;

namespace SecureCommander.GUI
{
    /// <summary>
    /// Interaction logic for FormVerifySignature.xaml
    /// </summary>
    public partial class FormVerifySignature : Window
    {
        public string StartDirectory { get; set; }
        public UserInfo User { get; set; }
        public string Passphrase { get; set; }
        public List<UserInfo> Users { get; set; }

        public FormVerifySignature()
        {
            InitializeComponent();
        }

        private void btnBrowseInput_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog opendiasrc = new OpenFileDialog();
            if (opendiasrc.ShowDialog().Value)
            {
                txtInput.Text = opendiasrc.FileName;
                txtOutput.Text = txtInput.Text + ".sig";
            }
        }

        private void btnBrowseOutput_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.InitialDirectory = StartDirectory;
            saveDialog.Filter = "Signature|*.sig";
            if (saveDialog.ShowDialog().Value)
            {
                txtOutput.Text = saveDialog.FileName;
            }
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                UserInfo user = (UserInfo)cbbUser.SelectedItem;
                byte[] bytes = File.ReadAllBytes(txtInput.Text);
                byte[] sig = File.ReadAllBytes(txtOutput.Text);
                String pubkey = user.Publickey;

                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                rsa.FromXmlString(pubkey);
                byte[] HashValue;
                HashAlgorithm hash = new SHA256Managed();
                string CryConfig = CryptoConfig.MapNameToOID("SHA256");
                HashValue = hash.ComputeHash(bytes);
                if (rsa.VerifyHash(HashValue, CryConfig, sig))
                {
                    FileHelper.ShowMessage("Verify successful!\nAccount:" + user.Name + "\nMail:" + user.Mail, MessageBoxImage.Information);
                    e.Handled = true;
                    DialogResult = true;
                    this.Close();
                }
                else
                {
                    FileHelper.ShowMessage("Validation Fail!\nThe signature is not belong to user:" + user.Name + "\nMail:" + user.Mail, MessageBoxImage.Warning);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            cbbUser.ItemsSource = Users;
        }
    }
}
